/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package downloadmanager.component;

import downloadmanager.event.XEventListener;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Administrator
 */
public class MultiSegmentProgressBar extends JPanel {

    ProgressSegmentList segmentList;
    XEventListener<Object> repaintAction;

    public MultiSegmentProgressBar() {
        final MultiSegmentProgressBar self = this;
        repaintAction = new XEventListener<Object>() {

            public void eventFired(Object sender, Object args) {
                EventQueue.invokeLater(new Runnable() {

                    public void run() {
                        repaint();
                    }
                });

            }
        };
    }

    public void setSegmentList(ProgressSegmentList segmentList) {
        if (this.segmentList != null) {
            this.segmentList.ValueChange.unsubscribe(repaintAction);
        }
        this.segmentList = segmentList;
        if (segmentList != null) {
            this.segmentList.ValueChange.subscribe(repaintAction);
        }
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.white);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.blue);
        ProgressSegmentList segmentList = this.segmentList;
        synchronized (segmentList) {
            if (segmentList != null) {
                for (ProgressSegment s : segmentList.getSegments()) {
                    paintSegment(g, s);
                }
            }
        }
    }

    private void paintSegment(Graphics g, ProgressSegment s) {
        if (segmentList.getMax() == segmentList.getMin()) {
            //the segmentList is not truly instantiated, let's wait
            return;
        }
        double start = (s.getStart() - segmentList.getMin()) / (segmentList.getMax() - segmentList.getMin()) * getWidth();
        double end = (s.getEnd() - segmentList.getMin()) / (segmentList.getMax() - segmentList.getMin()) * getWidth() - start;
        g.drawLine((int) Math.round(start), 0, (int) Math.round(start), getHeight());
        //g.drawRect((int) Math.round(start), 0, (int) Math.round(end), getHeight());
        g.fillRect((int) Math.round(start), 0, (int) Math.round(end), getHeight());
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setTitle("Test Progress");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MultiSegmentProgressBar bar = new MultiSegmentProgressBar();
        f.setLayout(new FlowLayout(FlowLayout.LEFT));
        bar.setPreferredSize(new Dimension(300, 20));
        f.add(bar);
        f.pack();
        f.setVisible(true);
        ProgressSegmentList smList = new ProgressSegmentList();
        bar.setSegmentList(smList);
        smList.setMin(0);
        smList.setMax(100);
        smList.addProgressSegments(new ProgressSegment[]{new ProgressSegment(0, 10), new ProgressSegment(20, 30), new ProgressSegment(40, 50), new ProgressSegment(60, 70), new ProgressSegment(80, 90)});
    }
}
